package middleware

import (
	"time"

	"github.com/gin-gonic/gin"
	"github.com/rs/cors"
)

func Cors() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		cors.New(cors.Options{
			//AllowAllOrigins:  true,
			AllowedOrigins:   []string{"*"}, // 等同于允许所有域名 #AllowAllOrigins:  true
			AllowedMethods:   []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
			AllowedHeaders:   []string{"*", "Authorization"},
			ExposedHeaders:   []string{"Content-Length", "text/plain", "Authorization", "Content-Type"},
			AllowCredentials: true,
			MaxAge:           int(12 * time.Hour),
		})
	}
}
